from .base import CVSSLevel, level_from_score, level_number_from_score
from .cvss2 import calculate_score_cvss2, is_cvss2
from .cvss3 import calculate_score_cvss3_0, calculate_score_cvss3_1, is_cvss3_0, is_cvss3_1
from .cvss4 import calculate_score_cvss4_0, is_cvss4_0


def is_cvss(vector):
    return is_cvss4_0(vector) or is_cvss3_1(vector) or is_cvss3_0(vector) or is_cvss2(vector)


def calculate_metrics(vector) -> dict:
    if (metrics := calculate_score_cvss4_0(vector)) is not None:
        return metrics
    if (metrics := calculate_score_cvss3_1(vector)) is not None:
        return metrics
    elif (metrics := calculate_score_cvss3_0(vector)) is not None:
        return metrics
    elif (metrics := calculate_score_cvss2(vector)) is not None:
        return metrics
    return {
        "version": None,
        "base": {
            "score": 0.0,
            "exploitability": 0.0,
            "impact": 0.0,
        },
        "temporal": {
            "score": 0.0,
            "exploitability": 0.0,
            "impact": 0.0,
        },
        "environmental": {
            "score": 0.0,
            "exploitability": 0.0,
            "impact": 0.0,
        },
        "final": {
            "score": 0.0,
            "exploitability": 0.0,
            "impact": 0.0,
        },
    }


def calculate_score(vector) -> float:
    """
    Calculate the CVSS score from a CVSS vector.
    Supports CVSS v2, v3.0 and v3.1
    """
    return calculate_metrics(vector)['final']['score']


__all__ = [
    'CVSSLevel', 'is_cvss', 'level_from_score', 'level_number_from_score', 'calculate_metrics', 'calculate_score',
]
